package cn.jcet.module.sys.controller;

import com.google.code.kaptcha.Producer;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@Controller
@RequestMapping("sys")
public class SystemController {


    @Autowired
    private Producer producer;

    /**
     * 跳转登陆页面
     * @return
     */
    @GetMapping("toLogin")
    public String toLogin(){
        return "system/main/login";
    }

    /**
     * 生成验证码
     * @param response
     * @param session
     */
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, HttpSession session) {
        try {
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/jpeg");
            //生成文字验证码
            String text = producer.createText();
            //生成图片验证码
            BufferedImage image = producer.createImage(text);
            //保存到shiro session
            //ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY,text);
            session.setAttribute("code",text);
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
        }catch (Exception e){
        }
    }

    /**
     * 跳转到首页
     */
    @GetMapping("index")
    public String toIndex(){
        return  "system/main/index";
    }

    /**
     * 跳转工作台
     */
    @GetMapping("toDeskManager")
    public String toDeskManager(){
        return "system/main/deskManager";
    }

    /**
     * 跳转日志
     */
    @GetMapping("toLogInfoManager")
    public String toLogInfoManager(){
        return "system/loginfo/loginfoManager";
    }

    /**
     * 跳转公告
     */
    @GetMapping("toNoticeManager")
    public String toNoticeManager(){
        return "system/notice/noticeManager";
    }

    /**
     * 跳转部门管理
     */
    @GetMapping("toDeptManager")
    public String toDeptManager(){
        return "system/dept/deptManager";
    }

    /**
     * 跳转到部门管理 -Left
     */
    @GetMapping("toDeptLeft")
    public String toDeptLeft(){
        return "system/dept/deptLeft";
    }

    /**
     * 跳转到部门管理 -Right
     */
    @GetMapping("toDeptRight")
    public String toDeptRight(){
        return "system/dept/deptRight";
    }


    /**
     * 菜单管理
     * @return
     */
    @GetMapping("toMenuManager")
    public String toMenuManager(){
        return "system/menu/menuManager";
    }

    /**
     * 跳转左侧菜单栏
     * @return
     */
    @GetMapping("toMenuLeft")
    public String toMenuLeft(){
        return "system/menu/menuLeft";
    }

    /**
     * 跳转右侧菜单详细栏
     * @return
     */
    @GetMapping("toMenuRight")
    public String toMenuRight(){
        return "system/menu/menuRight";
    }

    /**
     * 权限管理
     * @return
     */
    @GetMapping("toPermissionManager")
    public String toPermissionManager(){
        return "system/permission/permissionManager";
    }

    /**
     * 菜单管理左侧
     * @return
     */
    @GetMapping("toPermissionLeft")
    public String toPermissionLeft(){
        return "system/permission/permissionLeft";
    }

    /**
     * 菜单右侧
     * @return
     */
    @GetMapping("toPermissionRight")
    public String toPermissionRight(){
        return "system/permission/permissionRight";
    }

    /**
     * 跳转角色管理
     * @return
     */
    @GetMapping("toRoleManager")
    public String toRoleManager(){
        return "system/role/roleManager";
    }

    /**
     * 跳转用户管理
     * @return
     */
    @GetMapping("toUserManager")
    public String toUserManager(){
        return "system/user/userManager";
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:toLogin";
    }

    /**
     * 缓存页面
     * @return
     */
    @GetMapping("toCacheManager")
    public String toCacheManager(){
        return "system/cache/cacheManager";
    }

    /**
     * 跳转修改密码
     * @return
     */
    @GetMapping("toChangePwd")
    public String toChangePwd(){
        return "system/user/changePwd";
    }



}